package com.codename1.samples;

import com.codename1.components.SpanLabel;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.CheckBox;
import com.codename1.ui.Component;
import com.codename1.ui.PickerComponent;
import com.codename1.ui.TextField;
import com.codename1.ui.spinner.Picker;
import java.util.Calendar;

import java.util.Date;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename
 * One</a> for the purpose of building native mobile applications using Java.
 */
public class StringPickerSample {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if (err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });
    }

    public void start() {
        if (current != null) {
            current.show();
            return;
        }
        testStringPicker();
    }

    public void stop() {
        current = getCurrentForm();
        if (current instanceof Dialog) {
            ((Dialog) current).dispose();
            current = getCurrentForm();
        }
    }

    public void destroy() {
    }

    private void testStringPicker() {
        Form hi = new Form("Hi World", BoxLayout.y());
        Picker languagePicker = new Picker();
        languagePicker.setName("LanguagePicker");
        languagePicker.setType(Display.PICKER_TYPE_STRINGS);
        languagePicker.setStrings("Italian", "English", "German");
        languagePicker.setSelectedString("English");
        languagePicker.addActionListener((ev) -> {
            Log.p("Action Event fired");
            String newLanguage = languagePicker.getSelectedString();
            if (newLanguage != null && newLanguage.length() > 0) {
                Log.p("Language selected: " + newLanguage);
            }
        });

        Picker datePicker = new Picker();
        datePicker.setType(Display.PICKER_TYPE_DATE);

        Picker timePicker = new Picker();
        timePicker.setType(Display.PICKER_TYPE_TIME);

        Picker dateTimePicker = new Picker();
        dateTimePicker.setType(Display.PICKER_TYPE_DATE_AND_TIME);

        Picker durationPicker = new Picker();
        durationPicker.setType(Display.PICKER_TYPE_DURATION);

        Picker durationHoursPicker = new Picker();
        durationHoursPicker.setType(Display.PICKER_TYPE_DURATION_HOURS);

        Picker durationMinutesPicker = new Picker();
        durationMinutesPicker.setType(Display.PICKER_TYPE_DURATION_MINUTES);

        Picker calendarPicker = new Picker();
        calendarPicker.setType(Display.PICKER_TYPE_CALENDAR);

        CheckBox lightweight = new CheckBox("LightWeight");
        lightweight.setSelected(languagePicker.isUseLightweightPopup());
        lightweight.addActionListener(e -> {
            languagePicker.setUseLightweightPopup(lightweight.isSelected());
            datePicker.setUseLightweightPopup(lightweight.isSelected());
            timePicker.setUseLightweightPopup(lightweight.isSelected());
            dateTimePicker.setUseLightweightPopup(lightweight.isSelected());
            durationPicker.setUseLightweightPopup(lightweight.isSelected());
            durationHoursPicker.setUseLightweightPopup(lightweight.isSelected());
            durationMinutesPicker.setUseLightweightPopup(lightweight.isSelected());
            calendarPicker.setUseLightweightPopup(lightweight.isSelected());

        });
        TextField tf = new TextField();
        createFocusChain(tf, languagePicker, datePicker, timePicker, lightweight, dateTimePicker, durationPicker, durationHoursPicker, durationMinutesPicker, calendarPicker);

        hi.add(tf);
        hi.add(languagePicker);
        hi.add(datePicker);
        hi.add(timePicker);
        hi.add(lightweight);
        hi.add(dateTimePicker);
        hi.add(durationPicker);
        hi.add(durationHoursPicker);
        hi.add(durationMinutesPicker);
        hi.add(calendarPicker);
        PickerComponent gender = PickerComponent.createStrings("Male", "Female", "Other", "Don't ask me").label("Gender");
        gender.getPicker().setUseLightweightPopup(true);

        PickerComponent date = PickerComponent.createDate(new Date()).label("Birthday");
        date.getPicker().setUseLightweightPopup(true);
        hi.add(gender).add(date);

        hi.add(new SpanLabel("Date Time between July 7 and July 12 2019"));
        Picker rangePicker1 = new Picker();
        rangePicker1.setType(Display.PICKER_TYPE_DATE_AND_TIME);
        System.out.println("Start date " + newDate(119, 6, 7));
        rangePicker1.setStartDate(newDate(119, 6, 7));
        rangePicker1.setEndDate(newDate(119, 6, 12));
        hi.add(rangePicker1);

        hi.add(new SpanLabel("Date Time between July 7 and July 12 2019 between 8am and 11am"));

        rangePicker1 = new Picker();
        rangePicker1.setType(Display.PICKER_TYPE_DATE_AND_TIME);
        System.out.println("Start date " + newDate(119, 6, 7));
        rangePicker1.setStartDate(newDate(119, 6, 7));
        rangePicker1.setEndDate(newDate(119, 6, 12));
        rangePicker1.setHourRange(8, 11);
        hi.add(rangePicker1);

        hi.add(new SpanLabel("Date Time between July 7 and July 12 2019 between 8am and 11am, with current time at 9am"));

        rangePicker1 = new Picker();
        rangePicker1.setType(Display.PICKER_TYPE_DATE_AND_TIME);
        System.out.println("Start date " + newDate(119, 6, 7));
        rangePicker1.setStartDate(newDate(119, 6, 7));
        rangePicker1.setEndDate(newDate(119, 6, 12));
        rangePicker1.setHourRange(8, 11);
        rangePicker1.setDate(newDate(119, 6, 8, 9));

        hi.add(rangePicker1);

        hi.add(new SpanLabel("Date Time with current time at 9am"));

        rangePicker1 = new Picker();
        rangePicker1.setType(Display.PICKER_TYPE_DATE_AND_TIME);

        rangePicker1.setDate(newDate(118, 6, 8, 9));

        hi.add(rangePicker1);

        hi.show();
    }

    private Date newDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year + 1900);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    private Date newDate(int year, int month, int day, int hour) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year + 1900);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    private void createFocusChain(Component... cmps) {
        int len = cmps.length;
        for (int i = 0; i < len; i++) {
            if (i < len - 1) {
                cmps[i].setNextFocusDown(cmps[i + 1]);
            }
            if (i > 0) {
                cmps[i].setNextFocusUp(cmps[i - 1]);
            }
        }
    }

}
